package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.IWechatService;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class WechatServiceImpl implements IWechatService {

    private static final String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session";
    private static final String appid = "wx0c710222589d06ca";
    private static final String secret = "c4c2d0baafb4700e995fe25bf809bcd6";

    private static final String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";

    /**
     * 获取openid
     *
     * @param code
     * @return
     */
    @Override
    public String getOpenid(String code) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("appid", appid);
        params.put("secret", secret);
        params.put("js_code", code);
        params.put("grant_type", "authorization_code");

//        GET https://api.weixin.qq.com/sns/jscode2session
        HttpResponse response = HttpUtil.createRequest(Method.GET, OPENID_URL)
                .form(params)
                .execute();
        if (response.isOk()) {
            String body = response.body();
            JSONObject entries = JSONUtil.parseObj(body);
            return entries.getStr("openid");
        }
        return null;
    }

    /**
     * 获取手机号
     *
     * @param detailCode
     * @return
     */
    @Override
    public String getPhone(String detailCode) {
//        POST https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN
        String ACCESS_TOKEN = this.getToken();
        HashMap<String, Object> params = new HashMap<>();
        params.put("code", detailCode);
        HttpResponse response = HttpUtil.createRequest(Method.POST, PHONE_URL + ACCESS_TOKEN)
                .body(JSONUtil.toJsonStr(params))
                .execute();
        if (response.isOk()) {
            String body = response.body();
            JSONObject entries = JSONUtil.parseObj(body);
            return entries.getByPath("phone_info.phoneNumber", String.class);
        }

        return null;
    }

    private String getToken() {
        //GET https://api.weixin.qq.com/cgi-bin/token
        HashMap<String, Object> params = new HashMap<>();
        params.put("grant_type", "client_credential");
        params.put("appid", appid);
        params.put("secret", secret);
        HttpResponse response = HttpUtil.createRequest(Method.GET, TOKEN_URL)
                .form(params)
                .execute();
        if (response.isOk()) {
            String body = response.body();
            JSONObject entries = JSONUtil.parseObj(body);
            return entries.getStr("access_token");
        }
        return null;
    }
}
